package com.smfx.yxt;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.smfx.yxt.common.Const;
import com.smfx.yxt.model.ClassTeachers;
import com.smfx.yxt.model.OrganizeDepart;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.reqVo.BaseQueryVO;
import com.smfx.yxt.service.IClassTeachersService;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.utils.StringUtil;
import com.smfx.yxt.wechat.utils.WechatGlobalMap;

public class TeacherPositionService extends BaseTest {
	
	@Autowired
	private ITeacherService teacherService;
	@Autowired
	private IClassTeachersService classTeachersService;
	
	@Test
	public void start() {
		try {
			List<TeacherInfo> list = teacherService.selectList();
			BaseQueryVO vo = new BaseQueryVO();
			vo.setFinishYear(2013);
			vo.setSchoolId(1);
			List<ClassTeachers> ctList = classTeachersService.selectClassList(new BaseQueryVO());
			Map<String, OrganizeDepart> map = (Map<String, OrganizeDepart>) WechatGlobalMap.get(Const.CACHE_ORGANIZE_DEPART).getValue();
			for(TeacherInfo info : list) {
				List<String> plList = new ArrayList<>();
				String position = info.getPosition();
				if(StringUtils.isNotBlank(position)) {
					String[] strings = position.split(",");
					for(int i = 0; i < strings.length; i++) {
						if(!(strings[i].equals("3005") || strings[i].equals("3007") ||
								strings[i].equals("3010"))) {
							plList.add(strings[i]);
						}
					}
				}
				for(ClassTeachers ct : ctList) {
					if(ct.getClassTeacherId().intValue() == info.getId().intValue()) {
						plList.add("3005");
					}else if(ct.getDeputyClassTeacher().equals(info.getName())) {
						plList.add("3010");
					}else if(ct.getLifeTeacher().equals(info.getName())) {
						plList.add("3007");
					}
				}
				List<String> dList = new ArrayList<>();
				for(int i = 0; i < plList.size(); i++) {
					if(map.containsKey(plList.get(i))){
						dList.add(String.valueOf(map.get(plList.get(i)).getParentId()));
					}
				}
				info.setDepart(dList.size() > 0 ? String.join(",", dList) : "");
				info.setPosition(plList.size() > 0 ? String.join(",", plList) : "");
				teacherService.updateById(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
